package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.store.Course;
import com.itheima.mapper.store.CourseMapper;
import com.itheima.service.CourseService;
import com.itheima.service.CourseService;
import com.itheima.utils.MapperUtil;
import com.itheima.utils.UidUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

//CourseService实现类
public class CourseServiceImpl implements CourseService {
    /**
     * 查找所有
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Course> findAll(String pageNum,String pageSize) {
        SqlSession session = MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        int pageN=1;
        int pageS=10;
        if(StringUtils.isNotBlank(pageNum)){
            pageN=Integer.parseInt(pageNum);
        }
        if(StringUtils.isNotBlank(pageSize)){
            pageS=Integer.parseInt(pageSize);
        }
        PageHelper.startPage(pageN,pageS);
        List<Course> list=mapper.findAll();
        MapperUtil.close(session);
        return list;
    }
    //查询所有上级部门名称
    @Override
    public List<Course> list() {
        SqlSession session= MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        List<Course> list=mapper.findAll();
        MapperUtil.close(session);
        return list;
    }

    /**
     * 新增(其中ID(string类型)用UUID对象生成)
     * @param course
     */
    @Override
    public void save(Course course) {
        SqlSession session = MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        String uid = UidUtil.getUid();
        course.setId(uid);
        course.setCreateTime(new Date());
        mapper.save(course);
        MapperUtil.close(session);
    }

    /**
     * 根据多ID删除
     * @param id
     */
    @Override
    public boolean deleteByIds(String id) {
        SqlSession session=null;
        boolean flag=false;//因为有外键关联,所以可能会删除失败
        try {
            session = MapperUtil.getSession();
            CourseMapper mapper = session.getMapper(CourseMapper.class);
            String[] idArray = id.split(",");
            mapper.deleteByIds(idArray);
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MapperUtil.close(session);
        }
        return flag;
    }

    /**
     * 根据id查找对象
     * @param id
     * @return
     */
    @Override
    public Course findById(String id) {
        SqlSession session = MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        Course course=mapper.findById(id);
        MapperUtil.close(session);
        return course;
    }

    /**
     * 更新对象数据
     * @param course
     */
    @Override
    public void update(Course course) {
        //1、获取sesssion
        SqlSession session = MapperUtil.getSession();
        //2、获取代理对象
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        mapper.update(course);
        //3、关闭资源
        MapperUtil.close(session);

    }
}
